//
//  CSJDPayViewController.m
//  SHCloudStore
//
//  Created by SH_iOS on 2017/4/12.
//  Copyright © 2017年 SH. All rights reserved.
//
#import "UserModel.h"
#import "JDPayViewController.h"
#import "MyOrderViewController.h"
#import "CSJDPayViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
@interface CSJDPayViewController ()<UIWebViewDelegate>
@property(strong,nonatomic)UIWebView *webView;



@end

@implementation CSJDPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"京东快捷支付";
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:__I6Width(18)]}];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setImage:[UIImage imageNamed:@"csback"] forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 60, 30);
    // 让按钮内部的所有内容左对齐
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    // 修改导航栏左边的item
    UIBarButtonItem *nagetiveSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    
    nagetiveSpace.width = __I6Width(-10);
    self.navigationItem.leftBarButtonItems = @[nagetiveSpace,backBarBtn,];
    

    UIButton *shutBtn = [[UIButton alloc]init];
    [shutBtn  setTitle:@"关闭" forState:UIControlStateNormal];
    shutBtn.frame = CGRectMake(0, 0, __I6Width(40), __I6Width(40));
    shutBtn.titleLabel.font = KSystemFont(__I6Width(14));
    [shutBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [shutBtn addTarget:self action:@selector(shutWebView) forControlEvents:UIControlEventTouchUpInside];
//    [self.navigationBar addSubview:shutBtn];
//    
//    [shutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        
//        make.right.equalTo(self.navigationBar.mas_right).with.offset(__I6Width(-10));
//        make.centerY.equalTo(self.navigationBar.mas_centerY);
//        make.height.mas_equalTo(__I6Width(40));
//        make.width.mas_equalTo(__I6Width(40));
//        
//        
//    }];
    shutBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:shutBtn];
    
    [self configUI];

}

- (UIStatusBarStyle)preferredStatusBarStyle{
    
    return UIStatusBarStyleDefault;
}

- (void)back:(UIButton *)btn{
    if (self.webView.canGoBack)
    {
        [self.webView goBack];
    }else{
        
        [self.navigationController popViewControllerAnimated:YES];

    }
    
}


- (void)shutWebView{
    
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    
}



#pragma mark -- 初始化UI
- (void)configUI{
    
//    self.navigationBar.image = [UIImage imageNamed:@"navgationBar@2x"];
//    self.statusBar.image = [UIImage imageNamed:@"navgationBar@2x"];
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth,.5)];
    line.backgroundColor = KNAVIGATIONLINECOLOR;
    [self.view addSubview:line];
//    //[self.back setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
//
//    self.title = @"京东快捷支付";
    self.view.backgroundColor = [UIColor whiteColor];
    _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0,.5,KScreenWidth,KScreenHeight - KNAVIGATIONBARHEIGHT)];
    //设置代理
    _webView.delegate = self;
    //缩放整个scrollview
    _webView.scalesPageToFit = YES;
    
    //加载网页
    NSURL *url = [NSURL URLWithString:@"https://h5pay.jd.com/jdpay/saveOrder"];
    NSString *body = [NSString stringWithFormat:@"%@",self.jdhtml];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
    [request setHTTPMethod: @"POST"];
    [request setHTTPBody: [body dataUsingEncoding:NSUTF8StringEncoding]];
    [_webView loadRequest: request];
    [self.view addSubview:_webView];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];

    
}

#pragma mark - WebViewDelegate
//加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    NSLog(@"%@",error.localizedDescription);
    [MBProgressHUD hideHUDForView:self.view animated:YES];
}

//网页加载完成
- (void)webViewDidFinishLoad:(UIWebView *)webView
{

    [MBProgressHUD hideHUDForView:self.view animated:YES];
    JSContext *context = [self.webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    
    
    //定义好JS要调用的方法, share就是调用的share方法名
    //分享app
    context[@"shareOrder"] = ^() {
        
        NSArray *args = [JSContext currentArguments];
        
        for (JSValue *jsVal in args) {
            NSLog(@"%@", jsVal.toString);
        }
        
        NSLog(@"-------End Log-------%@",args);
        
        dispatch_async(dispatch_get_main_queue(), ^{
            [self csShareinviterUrl];
        });
        
        
    };
    //返回
    context[@"closeActivity"] = ^() {
        
        NSArray *args = [JSContext currentArguments];
        
        for (JSValue *jsVal in args) {
            NSLog(@"%@", jsVal.toString);
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
        
        
        
    };
    
    //查看订单
    context[@"checkOrder"] = ^() {
        
        NSArray *args = [JSContext currentArguments];
        
        for (JSValue *jsVal in args) {
            NSLog(@"%@", jsVal.toString);
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];
            [self.navigationController pushViewController:[[MyOrderViewController alloc]init] animated:YES];
        });
        
        
        
    };

    
    //跳转到
    context[@"bindDevice"] = ^() {
        
        NSArray *args = [JSContext currentArguments];
        
        for (JSValue *jsVal in args) {
            NSLog(@"%@", jsVal.toString);
        }
        dispatch_async(dispatch_get_main_queue(), ^{
           
        });
        
    };
    
    
    
}

#pragma mark --- 分享店铺
- (void)csShareinviterUrl{
  
    
}


@end
